//
//  ScrollerBaseViewController.m
//  YXLearnMasonry
//
//  Created by Rookie_YX on 16/10/27.
//  Copyright © 2016年 Rookie_YX. All rights reserved.
//

#import "ScrollerBaseViewController.h"

@interface ScrollerBaseViewController ()

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, strong) UILabel *lastL;  // 用来记录最后一个Lable

@end

@implementation ScrollerBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
  
  self.scrollView = [[UIScrollView alloc] init];
  self.scrollView.pagingEnabled = NO;
  self.scrollView.backgroundColor = [UIColor whiteColor];
  [self.view addSubview:self.scrollView];
  //  这里lastL必须要设置为nil,因为部位nil,初始布局没有frame,会crash
//  UILabel *lastL =nil;
  for (NSUInteger i = 0; i < 10; ++i) {
    UILabel *lable = [[UILabel alloc] init];
    lable.numberOfLines = 0;
    lable.layer.borderColor = [UIColor blueColor].CGColor;
    lable.layer.borderWidth = 1.0;
    lable.text = [self randomLabletext];
    
    // 最大布局宽度
    lable.preferredMaxLayoutWidth = kScreenWidth - 40;
    lable.textAlignment = 1;
    lable.textColor = RANDOMCOLOR;
    [self.scrollView addSubview:lable];
    // 布局
    [lable mas_makeConstraints:^(MASConstraintMaker *make) {
      make.left.equalTo(10);
     make.right.equalTo(self.view).with.offset(-10);
      
      if (_lastL) {
        make.top.equalTo(_lastL.bottom).offset(10);
      }
      else{
        make.top.equalTo(self.scrollView).offset(10);
      }
    }];
     _lastL = lable;
  }
  
  [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.edges.equalTo(self.view);
    
    // 让scrollview的contentSize随着内容的增多而变化
    make.bottom.equalTo(_lastL.mas_bottom).offset(20);
  }];
}

- (NSString *)randomLabletext{
  CGFloat textLength = arc4random() % 60 + 10;
  NSMutableString *str = [[NSMutableString alloc] init];
  for (NSUInteger i = 0; i < textLength; ++i) {
    [str appendFormat:@"最近加班严重,苦逼中,什么时候可以下班"];
  }
  return str;
}
@end
